package com.eques.demo.anim

import android.os.Bundle
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import com.eques.base.BaseActivity
import com.eques.demo.R
import com.eques.util.Config
import com.eques.view.OptPickerView
import com.eques.view.SelectTimeSaveDataImpl
import org.apache.commons.lang3.StringUtils
import org.json.JSONException
import org.json.JSONObject
import java.util.concurrent.Executors

class NotDisturbActivity : BaseActivity(), SelectTimeSaveDataImpl {
    private var tvStartTime: TextView? = null
    private var tvEndTime: TextView? = null
    private var rel_start: RelativeLayout? = null
    private var rel_end: RelativeLayout? = null
    private var tvMainLeftTopHint: TextView? = null
    private var tvMainTitleHint: TextView? = null
    private var tv_main_right_top_hint: TextView? = null
    private var timeType = 1
    private var work_mode = 0
    private var startTime: String? = null
    private var endTime: String? = null
    private var devId: String? = null
    private var start: String = ""
    private var end: String = ""

    //时间选择器
    private var optPickerView: OptPickerView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_not_disturb)
        initView()
        initTitleView()
        initData()
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    override fun initData() {
        work_mode = intent.getIntExtra("work_mode", 0)
        startTime = intent.getStringExtra("startTime")
        endTime = intent.getStringExtra("endTime")
        devId = intent.getStringExtra(Config.BID)
        if (StringUtils.isEmpty(startTime)) {
            startTime = "09:00"
        }
        if (StringUtils.isEmpty(endTime)) {
            endTime = "18:00"
        }
        tvStartTime!!.text = startTime
        tvEndTime!!.text = endTime
    }


    /**
     * 设置头部转态栏
     */
    fun initTitleView() {
        tvMainLeftTopHint!!.visibility = View.VISIBLE
        tvMainLeftTopHint?.setCompoundDrawablesWithIntrinsicBounds(
            resources.getDrawable(R.mipmap.ic_invitation_menu), null, null, null
        )
        tvMainTitleHint!!.visibility = View.VISIBLE

        tvMainTitleHint!!.setTextColor(resources.getColor(R.color.black))
        tvMainLeftTopHint?.setOnClickListener {
            finish()
        }
        tv_main_right_top_hint?.visibility = View.VISIBLE
        tv_main_right_top_hint?.setOnClickListener {
            setShaowOperation()
        }
    }

    private fun setShaowOperation() {
        //VL2 影子设置
        val jsonObject = JSONObject()
        try {
            jsonObject.put("disturb_status", work_mode.toString())
            jsonObject.put("disturb_start_time", startTime)
            jsonObject.put("disturb_end_time", endTime)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        Executors.newSingleThreadExecutor().submit {
            setInfo(jsonObject.toString(), devId!!)
        }
    }

    /**
     * 其它场景设置提醒时间段
     */
    fun setRemindTime(start: String?, type: Int) {
        // 容错，时间段为空用0补全，时间段字符长度不足四位，也用0补全
        var start = start
        if (StringUtils.isBlank(start)) {
            start = "0000"
        } else {
            if (start!!.length < 4) {
                for (i in 0..2) {
                    if (start.length < 4) {
                        start += "0"
                    }
                }
            }
        }
        optPickerView = OptPickerView.getInstance()
            .setActivity(this)
            .initNotifyTime(this)
            .initJumpTime(
                Integer.parseInt(start.substring(0, 2)),
                Integer.parseInt(start.substring(2))
            ).setTimeType(type)
            .show();

    }

    override fun saveTimeStr(timeStr: String) {
        if (timeType == 1) {
            start = timeStr;
        } else {
            end = timeStr;
        }
        setTimeStr();

    }

    /**
     * 按照格式设置开始时间和结束时间
     *
     * @author yc
     */
    fun setTimeStr() {
        if (StringUtils.isNotBlank(start) && start!!.length >= 3) {
            val startSb = StringBuilder(start)
            startTime = startSb.insert(2, ":").toString()
            tvStartTime!!.text = startTime
        }
        if (StringUtils.isNotBlank(end) && end!!.length >= 3) {
            val endSb = StringBuilder(end)
            endTime = endSb.insert(2, ":").toString()
            tvEndTime!!.text = endTime
        }
    }

    override fun initView() {
        tvStartTime = findViewById(R.id.tv_start_time)
        tvEndTime = findViewById(R.id.tv_end_time)
        rel_start = findViewById(R.id.rel_start)
        rel_end = findViewById(R.id.rel_end)
        tvMainLeftTopHint = findViewById(R.id.tv_main_left_top_hint)
        tvMainTitleHint = findViewById(R.id.tv_main_title_hint)
        tv_main_right_top_hint = findViewById(R.id.tv_main_right_top_hint)
        rel_start?.setOnClickListener {
            timeType = 1
            setRemindTime(start!!, timeType)
        }
        rel_end?.setOnClickListener {
            timeType = 2
            setRemindTime(end!!, timeType)
        }
    }
}